var qqmap = new (require("../../utils/qqmap-wx-jssdk.js"))({
  key: "SRLBZ-IMZ6J-SUIFA-FT724-N27WE-73BPK"
});
var util = require("../../utils/util.js");
// pages/index/index.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    myAddress:'',
    autoplay: true,
    duration: 500,
    interval: 2000,
    active1: '-active',
    carCount: 0,
    baseUrl: util.baseUrl,
    animat: "0",
    action: "0",
    hideBox: true,
    CarProductInfo: "",
    moveAction: '',
    goodsPage: 1,
    stopGetGoods: false,
    bannerList: '',
    systemCfg: '',
    productList: [],
    scrollTop: 0,
    showVideo: false,
    video: ''
  },
  Navigation: function (event) {
    var that = this;
    app.Navigation(event, that);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    wx.login({
      success: function (res) {
        util.djax({
          url: '/wxapp/login/doLogin',
          data: {
            code: res.code
          }
        }).then(function (res) {
          if (res.code != 1001) {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })
          } else {
            wx.setStorageSync('token', res.data.token)
            wx.setStorageSync('user', res.data)
            _this.initFun()
          }
        })
      }
    }),
    wx.getUserInfo({
      success: res => {
        var user = res.userInfo
        util.djax({
          url: '/wxapp/account/editInfo',
          data: {
            a: user.nickName,
            b: user.avatarUrl
          }
        })
      }
    })
  },
  onShow: function () {
   
  },
  initFun: function() {
    this.getSystemCfg()
    // 初始化购物车
    this.initCart();
    this.getGoods();
    this.setAlbum()
    this.getMyAddress()
  },
  getMyAddress: function() {
    var myAddress = wx.getStorageSync('myAddress')
    if (myAddress) {
      this.setData({
        myAddress: myAddress
      })
      return false
    }
    var _this = this
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        _this.getLocationAddress(res.latitude, res.longitude)
      },
    })

  },
  getSystemCfg: function() {
    var _this = this
    util.djax({
      url: '/wxapp/other/cfg'
    }).then(function(res){
      wx.setStorageSync('cfg', res.data)
      _this.setData({
        systemCfg: res.data
      })
      _this.setAlbum()
    })
  },
  setAlbum: function(){
    var _this = this
    util.djax({
      url: '/wxapp/other/flash'
    }).then(function(res){
      var imgList = res.data
      _this.setData({
        bannerList: imgList
      })
    })
  },
  showVideo: function(e){
    var video = e.currentTarget.dataset.video
    console.log(video)
    if (video) {
      this.setData({
        video: video,
        showVideo: true
      })
    }
  },
  closeVideo: function(){
    this.setData({
      showVideo: false
    })
  },
  onReachBottom: function() {
    this.getGoods();
  },
  getLocationAddress: function(lat,lgt) {
    var _this = this
    qqmap.reverseGeocoder({
      location: {
        latitude: lat,
        longitude: lgt
      },
      success: function(e) {
        wx.setStorageSync('myAddress', e.result.address)
        _this.setData({
          myAddress: e.result.address
        })
      }
    })
  },
  getGoods: function() {
    var _this = this;
    var page = _this.data.goodsPage
    if (this.data.stopGetGoods) {
      return false
    }
    util.djax({
      url: '/wxapp/Goods/Index',
      data: {
        page: page
      }
    }).then(function(res){
      if (res.code==1001) {
        var productList = _this.data.productList;
        var data = res.data.data
        if (data.length>0) {
          for (var i=0; i<data.length; i++) {
            productList.push(data[i])
          }
          _this.setData({
            productList: productList
          })
          page++;
          _this.setData({
            goodsPage: page
          })
        } else {
          _this.setData({
            stopGetGoods: true
          })
        }
      }
    })
  },
  showBig: function (t) {
    for (var e = this, a = t.currentTarget.dataset.id, o = e.data.productList, s = "", n = 0; n < o.length; n++) o[n].id == a && (s = o[n]);
    e.setData({
      productInfo: s,
      bigShow: !0
    });
  },
  bigHide: function () {
    this.setData({
      bigShow: !1
    });
  },
  addCar: function (e) {
    var _this = this
    util.djax({
      url: '/wxapp/Cart/addEdit',
      data: {
        a: e.currentTarget.dataset.id,
        b: 1
      }
    }).then(function(res){
        if (res.code!=1001) {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
          return 
        }
      var a = _this, o = e.detail.y - 25, s = e.detail.x - 25, n = a.data.carList, i = a.data.productsId, r = e.currentTarget.dataset.id, d = e.currentTarget.dataset.index, u = e.currentTarget.dataset.goodstype, c = a.data.productList;
      o = o - _this.data.scrollTop
      for (var h = "", v = 0; v < c.length; v++) c[v].id == r && (h = c[v]);
      u && (s = 114, o = 330), a.setData({
        animat: r,
        carTop: o,
        carLeft: s,
        bus_x: s - 90,
        bus_y: o - 130,
        hideBox: false,
        CarProductInfo: h,
        moveAction: "1"
      }), setTimeout(function () {
        a.setData({
          moveAction: "0",
          hideBox: true
        });
      }, 500);
      a.initCart();
    })
  },
  initCart: function() {
    var _this = this
    util.djax({
      url: '/wxapp/Cart/getCartCount'
    }).then(function(res){
      if (res.code != 1001) {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        return
      }
      var cartNumber = res.data>0?res.data:0
      console.log(cartNumber)  
      _this.setData({
        carCount: cartNumber
      })

    })
  },
  goAddress: function() {
    wx.navigateTo({
      url: '/pages/map/index',
    })
  } ,
  onPageScroll: function(e) {
     this.setData({
       scrollTop: e.scrollTop
     })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})